package com.qianbei.view.transport;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.qianbei.R;
import com.qianbei.bean.RequeastBean1;
import com.qianbei.bean.TransRequestBean;
import com.qianbei.bean.TransResultBean;
import com.qianbei.bean.TransportUpdateBean;
import com.qianbei.common.base.BaseActivity;
import com.qianbei.common.net.control.ServerControl;
import com.qianbei.common.net.control.ServerResult;
import com.qianbei.common.net.view.MsgAlert;
import com.qianbei.common.net.view.ProgressDialog;
import com.qianbei.common.util.DeviceUtil;
import com.qianbei.common.utilis.DateUtil;

import org.json.JSONArray;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * <pre>
 *     author : pwj
 *     e-mail : xxx@xx
 *     time   : 2019/02/21
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class TransportDetailActivity extends BaseActivity {
    @Bind(R.id.tv_bigbox)
    TextView tvBigbox;
    @Bind(R.id.tv_small_box)
    TextView tvSmallBox;
    @Bind(R.id.tv_name1)
    TextView tvName1;
    @Bind(R.id.tv_val1)
    TextView tvVal1;
    @Bind(R.id.tv_name2)
    TextView tvName2;
    @Bind(R.id.tv_val2)
    TextView tvVal2;
    @Bind(R.id.tv_all)
    TextView tvAll;
    @Bind(R.id.tv_code)
    TextView tvCode;
    @Bind(R.id.tv_company)
    TextView tvCompany;
    @Bind(R.id.ischeck)
    TextView ischeck;
    @Bind(R.id.sw)
    Switch sw;
    private List<TransResultBean> resultBeans;
    private boolean isError;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transport_detail);
        ButterKnife.bind(this);
        TrasportBean.ArrayBean.CustomerAryBean bean = (TrasportBean.ArrayBean.CustomerAryBean) getIntent().getSerializableExtra("key");
        if (bean == null) {
            bean = new TrasportBean.ArrayBean.CustomerAryBean();
        }
        setTitle("收运详情");
        mTitle.setrightTitle("保存");
        tvCompany.setText(bean.getCustomerName());
        if ("0".equals(bean.getStatus())) {
            ischeck.setText("未收运");
        } else if ("1".equals(bean.getStatus())) {
            ischeck.setText("收运中");
        } else {
            ischeck.setText("收运完成");
        }
        setDefaultData();
        initData(bean);
        sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isError = isChecked;
            }
        });
    }
    @SuppressLint("StaticFieldLeak")
    @Override
    public void fClick(int id) {
        super.fClick(id);
        switch (id) {
            case R.id.tv_right:
                postError(isError,0);
                break;
        }
    }
    private void postError(final boolean isError, final int index) {
        if (resultBeans == null || resultBeans.size() != 2) {
            new MsgAlert().show("请等待数据加载完成");
            return;
        }
        TransportUpdateBean updateBean = new TransportUpdateBean();
        TransportUpdateBean.BodyBean bodyBean = new TransportUpdateBean.BodyBean();
        bodyBean.setIsError(isError);
        TransportUpdateBean.WhereBean whereBean = new TransportUpdateBean.WhereBean();
        TransResultBean transResultBean = resultBeans.get(index);
        whereBean.set_id(transResultBean.get_id());
        updateBean.setBody(bodyBean);
        updateBean.setWhere(whereBean);
        RequeastBean1 bean = new RequeastBean1("update","com.cygps.dubbo.commons.data.ICommonDataService",
                updateBean,"WT_WorkOrder");
        if (!DeviceUtil.isNetworkAvailable()) {
            new MsgAlert().show("网络不好请稍后再试");
        } else {
            ServerControl sc = new ServerControl(bean);
            sc.serverListener = new ServerControl.ServerListener() {
                @Override
                public void serverFinish(ServerResult sr) {
                    if (sr.isContinue) {
                        if (index == 0) {
                            postError(isError, index + 1);
                        } else {
                            new MsgAlert().show("已经保存成功");
                            finish();
                        }
                        try {
                        } catch (Exception e) {
                            setDefaultData();
                            e.printStackTrace();
                        }
                    }
                }
            };
            new ProgressDialog(mContext).OKpost1(sc, "正在保存...");
        }
    }

    private void setDefaultData() {
        try {
            tvBigbox.setText("X 0");
            tvSmallBox.setText("X 0");
            tvAll.setText("0 kg");
            tvCode.setText("");
            tvName1.setText("感染性废物及其他");
            tvName2.setText("损伤性废物");
            tvVal1.setText("0 kg");
            tvVal2.setText("0 kg");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initData(TrasportBean.ArrayBean.CustomerAryBean customerAryBean) {
        if (!DeviceUtil.isNetworkAvailable()) {
            new MsgAlert().show("网络不好请稍后再试");
        } else {
            TransRequestBean bean = new TransRequestBean();
            bean.set_page(1);
            bean.set_number(1);
            bean.set_pageSize(10);
            bean.setDataSum("0");
            bean.set_sortField("date");
            bean.setCid(customerAryBean.getId());
            TransRequestBean.DateBean dateBean = new TransRequestBean.DateBean();
            String now = DateUtil.getNow("yyyy-MM-dd");
            dateBean.set$gte(now);
            dateBean.set$lte(now);
            bean.setDate(dateBean);
            final RequeastBean1 requeastBean =
                    new RequeastBean1("getWorkorderList", "com.cygps.dubbo.WasteTransport.IMapData",
                            bean, "WT_WorkOrder");
            ServerControl sc = new ServerControl(requeastBean);
            sc.serverListener = new ServerControl.ServerListener() {
                @Override
                public void serverFinish(ServerResult sr) {
                    if (sr.isContinue) {
                        try {
                            JSONArray js = sr.bodyData.optJSONArray("array");
                            resultBeans = JSON.parseArray(js.toString(), TransResultBean.class);
                            if (resultBeans == null || resultBeans.size() != 2) {
                                setDefaultData();
                            } else {
                                TransResultBean bean1 = resultBeans.get(0);
                                TransResultBean bean2 = resultBeans.get(1);
                                tvBigbox.setText("X " + bean1.getBox1());
                                tvSmallBox.setText("X " + bean1.getBox2());
                                tvAll.setText(bean1.getAllweight() + " kg");
                                tvCode.setText(bean1.getDuplicateNo());
                                tvName1.setText(bean1.getResName());
                                tvName2.setText(bean2.getResName());
                                tvVal1.setText(bean1.getWeight() + " kg");
                                tvVal2.setText(bean2.getWeight() + " kg");
                                isError = bean1.isError || bean2.isError;
                                sw.setChecked(isError);
                            }
                        } catch (Exception e) {
                            setDefaultData();
                            e.printStackTrace();
                        }
                    }
                }
            };
            new ProgressDialog(mContext).OKpost(sc, "正在加载...");
        }
    }
}
